package wechat

import (
	"fmt"
	"gitee.com/small_fish_official_net/more_wechat/model/https"
	"gitee.com/small_fish_official_net/more_wechat/model/times"
	"github.com/astaxie/beego/logs"
	"github.com/robertkrimen/otto"
)

// 检测是否已登录
//	输入值信息：
//	uuid:生成二维码的uuid值
//	返回值信息：
//	bool：是否成功，true为成功，false为失败继续检测
//	string：成功后的跳转地址，需要根据该地址进行下一步操作
func get_check(uuid string) (bool,string) {
	data,err := https.GetNoCookie("https://login.wx.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid="+uuid+"&tip=0&r="+
		fmt.Sprintf("%d",(^times.Timestamp() + 1))+"&_="+fmt.Sprintf("%d",times.Timestamp()))
	if err != nil {
		logs.Error("检测登录状态失败，原因："+err.Error())
		return false,""
	}
	vm := otto.New()
	vm.Run("window = {code:408};"+data)
	if value, err := vm.Get("window"); err == nil {
		if value,err := value.Object().Get("code");err == nil {
			switch value.String() {
			case "200":
				logs.Info("登录成功")
				value, _ := vm.Get("window");
				value,_ = value.Object().Get("redirect_uri")
				return true,value.String()
			case "201":
				logs.Info("已扫描二维码")
			case "400":
				logs.Info("二维码过期，请重新获取UUID进行扫描")
			case "408":
				logs.Info("心跳完成，继续下一次心跳")
			default:
				logs.Info("未知状态，默认重新获取")
			}
		}
	}
	return false,""
}